package servlet;

import bean.merch;
import bean.wareHouse;
import com.github.pagehelper.PageInfo;
import dao.impl.merchDaoImpl;
import dao.impl.supplierImpl;
import dao.impl.wareHouseImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/merchServlet")
public class merchServlet extends HttpServlet {
    private static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        String method=request.getParameter("method");
        //System.out.println("dada");
                        //merchAdd
        if(method.equals("merchAdd"))
        {
            System.out.println("进入添加方法");
            Date date1=new Date();
            int merchT= Integer.parseInt(request.getParameter("merchTypeld"));
            String merchN = request.getParameter("merchName");
            String merchD=request.getParameter("merchDate");
            //double merchP=Integer.parseInt(request.getParameter("merchPrice"));
            //这个应该是转成double  而不是int
            double merchP = Double.valueOf(request.getParameter("merchPrice"));
            System.out.println(merchT+" "+merchN+" "+merchD+" "+merchP);
            try {
                date1=sdf.parse(merchD);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            int insertMerch = new merchDaoImpl().merchAdd(merchT,merchN,date1,merchP);
            if(insertMerch==1){
                //重新加载 重定向
                response.sendRedirect("merchServlet?method=merchSelect");
                //request.getRequestDispatcher("merch/merchAdd.jsp").forward(request,response);
            }
            else{
                response.getWriter().println("添加失败");
            }
        }else if(method.equals("merchins")){
            System.out.println("a");
            request.getRequestDispatcher("merch/merchAdd.jsp").forward(request,response);
        }
                        //方法是merDel
        else if(method.equals("marchDel")){
            int delId = Integer.parseInt(request.getParameter("meId"));
            int delMerch= new merchDaoImpl().merchDel(delId);

            if(delMerch==1){
                response.sendRedirect("merchServlet?method=merchSelect");
            }

        }
        else if(method.equals("merchUpdate")){
            int meId = Integer.parseInt(request.getParameter("meId"));
            System.out.println(meId);
            Date date2=new Date();
            int merchT= Integer.parseInt(request.getParameter("merchTypeld"));
            String merchN = request.getParameter("merchName");
            String merchD=request.getParameter("merchDate");
            double merchP = Double.valueOf(request.getParameter("merchPrice"));
            //double merchP=Integer.parseInt(request.getParameter("merchPrice"));
            System.out.println(meId+"->"+merchT+" "+merchN+" "+merchD+" "+merchP);
            try {
                date2=sdf.parse(merchD);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            int updateMerch = new merchDaoImpl().merchUpdate(meId,merchT,merchN,date2,merchP);
            if(updateMerch==1){
                //重新加载 重定向
                response.sendRedirect("merchServlet?method=merchSelect");
            }
        }else if (method.equals("merchOne")){
            System.out.println("a");
            String meName = request.getParameter("meId");
            PageInfo meOne= new merchDaoImpl().merchOne(meName);
            System.out.println("-----------me-----------------------------");
            System.out.println(meOne);

            request.setAttribute("meList",meOne);
            request.getRequestDispatcher("/merch/merchSelect.jsp").forward(request,response);

        }
        else if(method.equals("merchSignal")){
            int meId = Integer.parseInt(request.getParameter("meId"));
            System.out.println("要修改的商品id->"+meId);
            merch meSignl= new merchDaoImpl().merchSingal(meId);
            System.out.println(meSignl);

            request.setAttribute("me",meSignl);
            request.getRequestDispatcher("merch/merchUpdate.jsp").forward(request,response);

        }else if(method.equals("merchSelect")){
            PageInfo pageInfo = new merchDaoImpl().meList(1,5);
            System.out.println(pageInfo);
            request.setAttribute("meList",pageInfo);
            request.getRequestDispatcher("merch/merchSelect.jsp").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
